package com.butuh.uang.tuh.ang.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.butuh.uang.tuh.ang.R;
import com.butuh.uang.tuh.ang.listener.OnClickDownListener;
import com.butuh.uang.tuh.ang.model.HomeListBean;
import com.butuh.uang.tuh.ang.util.DecimalFormatUtil;

import cn.droidlover.xdroidmvp.base.XListAdapter;

public class HomeListAdapter extends XListAdapter<HomeListBean> {

    OnClickDownListener onClickDownListener;


    public HomeListAdapter(Context context, OnClickDownListener onClickDownListener) {
        super(context);
        this.onClickDownListener = onClickDownListener;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        final ViewHloder viewHloder;
        if (convertView == null) {
            viewHloder = new ViewHloder();
            convertView = LayoutInflater.from(context).inflate(R.layout.item_home_list, null);
            viewHloder.iv_product_logo = convertView.findViewById(R.id.iv_product_logo);
            viewHloder.iv_download = convertView.findViewById(R.id.iv_download);
            viewHloder.iv_download.setTag(position);
            viewHloder.tv_product_name = convertView.findViewById(R.id.tv_product_name);
            viewHloder.tv_product_des = convertView.findViewById(R.id.tv_product_des);
            viewHloder.tv_product_money = convertView.findViewById(R.id.tv_product_money);
            viewHloder.tv_product_bunga = convertView.findViewById(R.id.tv_product_bunga);
            convertView.setTag(viewHloder);
        } else {
            viewHloder = (ViewHloder) convertView.getTag();
            viewHloder.iv_download.setTag(position);
        }
        final HomeListBean bean = data.get(position);
        Glide.with(context).load(bean.getIdentification()).into(viewHloder.iv_product_logo);
        viewHloder.tv_product_name.setText(bean.getDesignation());
        viewHloder.tv_product_des.setText(bean.getTagline());
        viewHloder.tv_product_money.setText("Rp " + DecimalFormatUtil.formatNumberWithMarkSplit(bean.getShowLargestAmountOfMoney(), 0));
        viewHloder.tv_product_bunga.setText((Double.parseDouble(bean.getDisplayTheLoanPeriod()) * 100 + "%").replaceAll("\\.",","));
        viewHloder.iv_download.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onClickDownListener.download(bean,position);
            }
        });
        return convertView;
    }

    class ViewHloder {
        ImageView iv_product_logo;
        ImageView iv_download;
        TextView tv_product_name;
        TextView tv_product_des;
        TextView tv_product_money;
        TextView tv_product_bunga;
    }

}
